﻿using SalesSys.DAL.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalesSys.DAL.Models
{
    /// <summary>
    /// 订单商品信息实体类
    /// </summary>
    public class CommodityInfo : DbEntityBase<Guid>
    {
        public class Mapper : SalMapper<CommodityInfo, Guid>
        {
            public Mapper()
            {//todo 记录应删除，但提示循环
                // HasMany(p => p.Records).WithOptional().WillCascadeOnDelete();
            }
        }

        /// <summary>
        /// 商品
        /// </summary>
        public virtual Commodity Commodity { get; set; }

        /// <summary>
        /// 数量
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// 价格
        /// </summary>
        public decimal Price { get; set; }
        /// <summary>
        /// 规范价，统一使用的非标价,算法：标件价-非标优惠
        /// </summary>
        public decimal NormPrice { get; set; }
        /// <summary>
        /// 总重
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal Weight { get; set; }

        /// <summary>
        /// 原始价格
        /// </summary>
        public decimal SourcePrice { get; set; }

        /// <summary>
        /// 原始重量
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal SourceWeight { get; set; }

        [Obsolete("商品记录都合并到订单记录中")]
        public virtual ICollection<OrderRecord> Records { get; set; }
        /// <summary>
        /// 所属订单
        /// </summary>
        public virtual Order Order { get; set; }
        /// <summary>
        /// 用于新建商品信息，通过商品和数量，计算其它值
        /// </summary>
        public void CalcNew()
        {
            SourcePrice = Price;
            SourceWeight = Commodity.Weight * Count;
            Price = SourcePrice;
            Weight = SourceWeight;
        }
        /// <summary>
        /// 商品描述
        /// </summary>
        public string CommodityFillName
        {
            get
            {
                if (Commodity != null) {
                    return Commodity.Name + "/" + Commodity.Standard + "/" + Commodity.Format;
                }
                else
                {
                    return "";
                }
            }
        }
    }
}
